package com.suning.sawp.service.impl.model;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.constants.ConfigConstants;
import com.suning.sawp.intf.model.BrandBean;
import com.suning.sawp.intf.model.CategoryBean;
import com.suning.sawp.intf.model.CommInfoBean;
import com.suning.sawp.intf.model.DownloadModelListBean;
import com.suning.sawp.intf.model.ModelBean;
import com.suning.sawp.intf.model.ModelCategoryBrandInfoBean;
import com.suning.sawp.intf.model.ModelCluster;
import com.suning.sawp.intf.model.ModelClusterInfo;
import com.suning.sawp.intf.model.ModelConstant;
import com.suning.sawp.intf.model.ModelEffectiveness;
import com.suning.sawp.intf.model.ModelExptBean;
import com.suning.sawp.intf.model.ModelManageDaoService;
import com.suning.sawp.intf.model.ModelTaskAssignDetailsBean;
import com.suning.sawp.intf.model.ModelTaskBean;
import com.suning.sawp.intf.model.ModelTaskMainBean;
import com.suning.sawp.intf.model.ModelTaskMonitorDetailVo;
import com.suning.sawp.intf.model.ModelTaskStoreBean;
import com.suning.sawp.intf.model.SaleModelExptBean;
import com.suning.sawp.intf.model.TaskInfoPart;
import com.suning.sawp.po.model.ModelCategoryBrandInfo;
import com.suning.sawp.po.model.ModelTaskMain;
import com.suning.sawp.service.util.BeanUtil;
import com.suning.sawp.service.util.DateUtils;
import com.suning.sawp.service.util.SCMConfigUtil;

@Service
public class ModelManageDaoServiceImpl implements ModelManageDaoService {

    @Resource
    DalClient dalClient;

    private static final String FULL_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    private static final String END_TIME_SUFFIXES = " 23:59:59";
    
    /**
     * 导出查询
     */
    private static final String EXPORT_SEARCH = "2";

    /**
     * 销量默认0
     */
    private static final Integer DEFALUT_COUNT = Integer.valueOf("0");

    @Override
    public List<ModelBean> queryModelByStoreCode(String regionCode, String branchCode, String commCode,
            String storeCode, Integer pageNo, Integer pageSize,String status,String categoryCode,String brandCode) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("regionCode", regionCode);
        if (StringUtils.isNotBlank(branchCode)) {
            queryMap.put("branchCode", branchCode);
        }
        if (StringUtils.isNotBlank(commCode)) {
            queryMap.put("commCode", commCode);
        }
        if (StringUtils.isNotBlank(storeCode)) {
            queryMap.put("storeCode", storeCode);
        }
        
        if (StringUtils.isNotBlank(status)) {
            queryMap.put("status", status);
        }
        
        if (StringUtils.isNotBlank(categoryCode)) {
            queryMap.put("categoryCode",categoryCode);
        }
        
        if (StringUtils.isNotBlank(brandCode)) {
            queryMap.put("brandCode", brandCode);
        }
        
        queryMap.put("startIndex", (pageNo - 1) * pageSize);
        queryMap.put("maxCount", pageSize);
        return dalClient.queryForList("MODEL_MAIN_LIST.QUERY_BY_STORE_CODE", queryMap, ModelBean.class);
    }

    @Override
    public int queryModelCountByStoreCode(String regionCode, String branchCode, String commCode, String storeCode,String status,String categoryCode,String brandCode) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("regionCode", regionCode);
        if (StringUtils.isNotBlank(branchCode)) {
            queryMap.put("branchCode", branchCode);
        }
        if (StringUtils.isNotBlank(commCode)) {
            queryMap.put("commCode", commCode);
        }
        if (StringUtils.isNotBlank(storeCode)) {
            queryMap.put("storeCode", storeCode);
        }
        if (StringUtils.isNotBlank(status)) {
            queryMap.put("status", status);
        }
        
        if (StringUtils.isNotBlank(categoryCode)) {
            queryMap.put("categoryCode",categoryCode);
        }
        
        if (StringUtils.isNotBlank(brandCode)) {
            queryMap.put("brandCode", brandCode);
        }
        Map<String, Object> numMap = dalClient.queryForMap("MODEL_MAIN_LIST.QUERY_COUNT_BY_STORE_CODE", queryMap);
        return numMap != null ? ((Number) numMap.get("NUM")).intValue() : 0;
    }

    @Override
    public List<ModelExptBean> queryModelExptByStoreCode(String regionCode, String branchCode, String storeCode,
            Integer pageNo, Integer pageSize, String searchType) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("disCode", regionCode);
        queryMap.put("orgCode", branchCode);
        queryMap.put("storeCode", storeCode);
        queryMap.put("startIndex", (pageNo - 1) * pageSize);
        queryMap.put("maxCount", pageSize);
        queryMap.put("unequalStatus", ModelConstant.PROBLEM_OPR_STATUS_OVER);
        // 导出需要查询门店类型
        if (EXPORT_SEARCH.equals(searchType)) {
            queryMap.put("storeTypes", 1);
        }
        // 展示降级开关标志 - 是否展示样机属性字段 -默认0为不打开
        String lowLevelFlag = SCMConfigUtil.getConfig(ConfigConstants.MODEL_MANAGE_SHOW_DEGRADE_FLAG,
                String.valueOf(ModelConstant.TASK_VALID_STATUS_DISABLE));
        queryMap.put("lowLevelFlag", lowLevelFlag);
        return dalClient.queryForList("MODEL_PROBLEM_LIST.QUERY_BY_STORE_CODE", queryMap, ModelExptBean.class);
    }

    @Override
    public int queryModelExptCountByStoreCode(String regionCode, String branchCode, String storeCode) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("disCode", regionCode);
        queryMap.put("orgCode", branchCode);
        queryMap.put("storeCode", storeCode);
        queryMap.put("unequalStatus", ModelConstant.PROBLEM_OPR_STATUS_OVER);
        Map<String, Object> numMap = dalClient.queryForMap("MODEL_PROBLEM_LIST.QUERY_COUNT_BY_STORE_CODE", queryMap);
        return numMap != null ? ((Number) numMap.get("NUM")).intValue() : 0;
    }

    @Override
    public List<SaleModelExptBean> querySaleModelExptByStoreCode(String regionCode, String branchCode,
            String storeCode, Integer pageNo, Integer pageSize, Integer isInStorage, String searchType) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("regionCode", regionCode);
        if (StringUtils.isNotBlank(branchCode)) {
            queryMap.put("branchCode", branchCode);
        }
        if (StringUtils.isNotBlank(storeCode)) {
            queryMap.put("storeCode", storeCode);
        }
        queryMap.put("startIndex", (pageNo - 1) * pageSize);
        queryMap.put("maxCount", pageSize);
        if (isInStorage != null) {
            queryMap.put("isInStorage", isInStorage);
        }
        if (EXPORT_SEARCH.equals(searchType)) {
            queryMap.put("storeTypes", "1");
        }
        return dalClient.queryForList("MODEL_PROBLEM_LIST.QUERY_SALE_BY_STORE_CODE", queryMap, SaleModelExptBean.class);
    }

    @Override
    public int querySaleModelExptCountByStoreCode(String regionCode, String branchCode, String storeCode, Integer isInStorage) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("regionCode", regionCode);
        if (StringUtils.isNotBlank(branchCode)) {
            queryMap.put("branchCode", branchCode);
        }
        if (StringUtils.isNotBlank(storeCode)) {
            queryMap.put("storeCode", storeCode);
        }
        if (isInStorage != null) {
            queryMap.put("isInStorage", isInStorage);
        }
        Map<String, Object> numMap = dalClient.queryForMap("MODEL_PROBLEM_LIST.QUERY_SALE_COUNT_BY_STORE_CODE",
                queryMap);
        return numMap != null ? ((Number) numMap.get("NUM")).intValue() : 0;
    }

    @Override
    public List<ModelTaskBean> queryTaskList(String startTime, String endTime, Set<Integer> taskType, Integer pageNo,
            Integer pageSize, String taskStatus) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("startTime", StringUtils.isNotBlank(startTime) ? startTime : null);
        queryMap.put("endTime", StringUtils.isNotBlank(endTime) ? endTime + END_TIME_SUFFIXES : null);
        queryMap.put("taskType", taskType);
        queryMap.put("startIndex", (pageNo - 1) * pageSize);
        queryMap.put("maxCount", pageSize);
        // 处理任务状态
        if (StringUtils.isNotBlank(taskStatus)) {
            if ("99".equals(taskStatus)) {
                queryMap.put("validStatus", "0");
            } else {
                queryMap.put("runStatus", taskStatus);
                queryMap.put("validStatus", "1");
            }
        }
        List<ModelTaskBean> list = dalClient.queryForList("MODEL_TASK.QUERY_TASK_LIST_BY_CON", queryMap,
                ModelTaskBean.class);
        if (CollectionUtils.isNotEmpty(list)) {
            for (ModelTaskBean task : list) {
                String categoryName = "";
                String brandName = "";
                queryMap.put("taskId", task.getTaskId());
                List<Map<String, Object>> brandList = dalClient.queryForList("MODEL_TASK.QUERY_CATE_BRAND_BY_TASKID",
                        queryMap);
                if (CollectionUtils.isNotEmpty(brandList)) {
                    StringBuilder cate = new StringBuilder();
                    StringBuilder brand = new StringBuilder();
                    Set<String> cateSet = new HashSet<String>();
                    for (Map<String, Object> map : brandList) {
                        if (!cateSet.contains(String.valueOf(map.get("categoryCode")))) {
                            cate.append(map.get("categoryName")).append(",");
                            cateSet.add(String.valueOf(map.get("categoryCode")));
                        }
                        brand.append(map.get("brandName")).append(",");
                    }
                    categoryName = cate.substring(0, cate.length() - 1);
                    brandName = brand.substring(0, brand.length() - 1);
                }
                task.setCategoryName(categoryName);
                task.setBrandName(brandName);
            }
        }
        return list;
    }

    @Override
    public int queryTaskListCount(String startTime, String endTime, Set<Integer> taskType, String taskStatus) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("startTime", StringUtils.isNotBlank(startTime) ? startTime : null);
        queryMap.put("endTime", StringUtils.isNotBlank(endTime) ? endTime + END_TIME_SUFFIXES : null);
        queryMap.put("taskType", taskType);
        // 处理任务状态
        if (StringUtils.isNotBlank(taskStatus)) {
            if ("99".equals(taskStatus)) {
                queryMap.put("validStatus", "0");
            } else {
                queryMap.put("runStatus", taskStatus);
                queryMap.put("validStatus", "1");
            }
        }
        Map<String, Object> numMap = dalClient.queryForMap("MODEL_TASK.QUERY_TASK_COUNT_BY_CON", queryMap);
        return numMap != null ? ((Number) numMap.get("NUM")).intValue() : 0;
    }

    @Override
    public List<CategoryBean> queryAllCate() {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        return dalClient.queryForList("MODEL_TASK.QUERY_CATEGORY", queryMap, CategoryBean.class);
    }

    @Override
    public List<BrandBean> queryAllBrand() {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        return dalClient.queryForList("MODEL_TASK.QUERY_BRAND", queryMap, BrandBean.class);
    }

    @Override
    public void batchInsertModelList(Map<String, Object>[] maps) {
        dalClient.batchUpdate("MODEL_MAIN_LIST.INSERT", maps);
    }

    @Override
    public long insertTask(ModelTaskMain task) {
        return dalClient.persist(task).longValue();
    }

    @Override
    public void insertModelCateBrand(ModelCategoryBrandInfo cateBrand) {
        dalClient.execute("MODEL_MAIN_LIST.INSERT_CATE_BRAND", BeanUtil.describe(cateBrand));
    }

    @Override
    public boolean checkTaskExist(List<String> storeList, List<String> brandList, String startTime, String endTime,
            Integer taskType, Long taskId, String storage) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("storeList", storeList);
        queryMap.put("brandList", brandList);
        queryMap.put("startTime", startTime);
        queryMap.put("endTime", endTime);
        queryMap.put("taskType", taskType);
        queryMap.put("storage", storage);
        if (taskId != null) {
            queryMap.put("taskId", taskId);
        }
        Map<String, Object> numMap = dalClient.queryForMap("MODEL_TASK.QUERY_IS_EXIST", queryMap);
        return ((Number) numMap.get("NUM")).intValue() > 0;
    }

    @Override
    public void batchInsertTaskStore(Map<String, Object>[] maps) {
        dalClient.batchUpdate("MODEL_TASK.INSERT_STORE", maps);
    }

    @Override
    public void batchInsertTaskBrand(Map<String, Object>[] maps) {
        dalClient.batchUpdate("MODEL_TASK.INSERT_BRAND", maps);
    }

    @Override
    public List<ModelCategoryBrandInfoBean> queryBrandList(List<String> brandCodes) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("branch9codes", brandCodes);
        return dalClient.queryForList("MODEL_MAIN_LIST.SELECT_BRAND_BY_BRAND_CODE", queryMap,
                ModelCategoryBrandInfoBean.class);
    }

    @Override
    public List<String> queryTaskStoreCodeList(Long taskId) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("taskId", taskId);
        return dalClient.queryForList("MODEL_TASK.QUERY_STORE_CODE_LIST", queryMap, String.class);
    }

    @Override
    public List<String> queryTaskBrand9CodeList(Long taskId) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("taskId", taskId);
        return dalClient.queryForList("MODEL_TASK.QUERY_BRAND_CODE_LIST", queryMap, String.class);
    }

    @Override
    public void delteTaskStoreByStoreCode(List<String> storeCodeList, Long taskId) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("taskId", taskId);
        queryMap.put("storeCodeList", storeCodeList);
        dalClient.execute("MODEL_TASK.DELETE_STORE_BY_CODE", queryMap);
    }

    @Override
    public void deleteTaskBrandByBrand9Code(List<String> brandCodeList, Long taskId) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("taskId", taskId);
        queryMap.put("branchCodeList", brandCodeList);
        dalClient.execute("MODEL_TASK.DELETE_BRAND_BY_CODE", queryMap);
    }

    @Override
    public boolean checkTaskIsUpdate(Long taskId) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("taskId", taskId);
        queryMap.put("runStatus", ModelConstant.TASK_RUN_STATUS_BEFORE);
        Map<String, Object> numMap = dalClient.queryForMap("MODEL_TASK.QUERY_TASK_COUNT_BY_CON", queryMap);
        return ((Number) numMap.get("NUM")).intValue() > 0;
    }

    @Override
    public ModelTaskMainBean queryModelTaskDetail(Long taskId) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("taskId", taskId);
        return dalClient.queryForObject("MODEL_TASK.SELECT_BY_TASK_ID", queryMap, ModelTaskMainBean.class);
    }

    @Override
    public List<BrandBean> queryTaskBrandByTaskId(Long taskId) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("taskId", taskId);
        return dalClient.queryForList("MODEL_TASK.SELECT_CATE_BRAND_BY_TASK_ID", queryMap, BrandBean.class);
    }

    @Override
    public void updateTaskMainByTaskId(ModelTaskMain modelTask, Long taskId) {
        Map<String, Object> updateMap = BeanUtil.describe(modelTask);
        updateMap.put("taskId", taskId);
        if (modelTask.getStartTime() != null) {
            updateMap.put("startTime", DateUtils.getDateStrByFormat(modelTask.getStartTime(), FULL_DATE_FORMAT));
        }
        if (modelTask.getEndTime() != null) {
            updateMap.put("endTime", DateUtils.getDateStrByFormat(modelTask.getEndTime(), FULL_DATE_FORMAT));
        }
        if (modelTask.getOprTime() != null) {
            updateMap.put("oprTime", DateUtils.getDateStrByFormat(modelTask.getOprTime(), FULL_DATE_FORMAT));
        }
        dalClient.execute("MODEL_TASK.UPDATE_TASK_BY_TASK_ID", updateMap);
    }

    @Override
    public void deleteTaskByTaskId(Long taskId) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("taskId", taskId);
        dalClient.execute("MODEL_TASK.DELETE_TASK_BY_TASK_ID", queryMap);
    }

    @Override
    public void deleteTaskStoreById(Long taskId) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("taskId", taskId);
        dalClient.execute("MODEL_TASK.DELETE_STORE_BY_CODE", queryMap);
    }

    @Override
    public void deleteTaskBrandById(Long taskId) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("taskId", taskId);
        dalClient.execute("MODEL_TASK.DELETE_BRAND_BY_CODE", queryMap);
    }

    @Override
    public List<ModelCluster> queryModelClusterList( String commInfo, 
            Integer pageNo, Integer pageSize) {
        Map<String, Object> queryMap = new HashMap<String, Object>();

        if (commInfo != null && StringUtils.isNotBlank(commInfo.trim())) {
            if (StringUtils.isNumeric(commInfo.trim()) && commInfo.trim().length() == 9) {
                queryMap.put("commCode", commInfo.trim());
            } else {
                queryMap.put("commName", "%" + commInfo.trim() + "%");
            }
        }
        queryMap.put("startIndex", pageSize * (pageNo - 1));
        queryMap.put("maxCount", pageSize);
        List<ModelCluster> list = new ArrayList<ModelCluster>();
        list = dalClient.queryForList("MODEL_COMMODITY_CLUSTER.QUERY_CLUSTERINFO_LIST_BY_CON", queryMap,
                ModelCluster.class);
        return list;
    }
    
    @Override
    public List<ModelClusterInfo> queryModelClusterInfoList( String commInfo, 
            Integer pageNo, Integer pageSize) {
        Map<String, Object> queryMap = new HashMap<String, Object>();

        if (commInfo != null && StringUtils.isNotBlank(commInfo.trim())) {
            if (StringUtils.isNumeric(commInfo.trim()) && commInfo.trim().length() == 9) {
                queryMap.put("commCode", commInfo.trim());
            } else {
                queryMap.put("commName", "%" + commInfo.trim() + "%");
            }
        }
        queryMap.put("startIndex", pageSize * (pageNo - 1));
        queryMap.put("maxCount", pageSize);
        List<ModelClusterInfo> list = new ArrayList<ModelClusterInfo>();
        list = dalClient.queryForList("MODEL_COMMODITY_CLUSTER.QUERY_CLUSTERINFO", queryMap,
                ModelClusterInfo.class);
        return list;
    }

    @Override
    public List<ModelClusterInfo> queryModelClusterDetailList(String branchCode, String commInfo, Integer isCluster,
            Integer pageNo, Integer pageSize) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
//        queryMap.put("orgCode", branchCode);
//        if (commInfo != null && StringUtils.isNotBlank(commInfo.trim())) {
//            if (StringUtils.isNumeric(commInfo.trim()) && commInfo.trim().length() == 9) {
//                queryMap.put("commCode", commInfo.trim());
//            } else {
//                queryMap.put("commName", "%" + commInfo.trim() + "%");
//            }
//        }
        if (null == pageNo) {
            pageNo = 1;
        }
        queryMap.put("startIndex", pageSize * (pageNo - 1));
        queryMap.put("maxCount", pageSize);
        List<ModelClusterInfo> list = dalClient.queryForList("MODEL_COMMODITY_CLUSTER.QUERY_CLUSTER_LIST_DETAIL_BY_CON", queryMap,
                ModelClusterInfo.class);
//        if (isCluster == ModelConstant.IS_CLUSTER_YES) {
//            list = dalClient.queryForList("MODEL_COMMODITY_CLUSTER.QUERY_CLUSTER_LIST_DETAIL_BY_CON", queryMap,
//                    ModelClusterInfo.class);
//        } else if (isCluster == ModelConstant.IS_CLUSTER_NO) {
//            list = dalClient.queryForList("MODEL_COMMODITY_CLUSTER.QUERY_NO_CLUSTER_LIST_BY_CON", queryMap,
//                    ModelClusterInfo.class);
//        }
        return list;
    }

    @Override
    public int queryModelClusterCount(String commInfo, boolean isClusterDetail) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        Map<String, Object> numMap = new HashMap<String, Object>();
        
        if (commInfo != null && StringUtils.isNotBlank(commInfo.trim())) {
            if (StringUtils.isNumeric(commInfo.trim()) && commInfo.trim().length() == 9) {
                queryMap.put("commCode", commInfo.trim());
            } else {
                queryMap.put("commName", "%" + commInfo.trim() + "%");
            }
        }
        
        if (!isClusterDetail) {
            numMap = dalClient.queryForMap("MODEL_COMMODITY_CLUSTER.QUERY_CLUSTERINFO_LIST_COUNT", queryMap);
        } else {
            numMap = dalClient.queryForMap("MODEL_COMMODITY_CLUSTER.QUERY_CLUSTERINFO_DETAIL_LIST_COUNT", queryMap);
        }
        return numMap.get("NUM") != null ? ((Number) numMap.get("NUM")).intValue() : 0;
    }

    /**
     * 查询未分配任务列表
     */
    @Override
    public List<ModelTaskAssignDetailsBean> queryTaskNoAssign(String regionCode, String branchCode, String storeCode,
            String cateCode, int pageSize, int pageNo, Long taskId) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        // 处理页码
        int start = pageSize * (pageNo - 1);
        if (start < 0) {
            start = 0;
        }
        queryMap.put("taskId", taskId);
        queryMap.put("startIndex", start);
        queryMap.put("maxCount", pageSize);
        if (StringUtils.isNotBlank(branchCode)) {
            queryMap.put("branchCode", branchCode);
        }
        if (StringUtils.isNotBlank(regionCode)) {
            queryMap.put("regionCode", regionCode);
        }
        if (StringUtils.isNotBlank(storeCode)) {
            queryMap.put("storeCode", storeCode);
        }
        if (StringUtils.isNotBlank(cateCode)) {
            queryMap.put("cateCode", cateCode);
        }
        return dalClient.queryForList("MODEL_TASK.QUERY_TASK_NO_ASSIGN", queryMap, ModelTaskAssignDetailsBean.class);
    }

    /**
     * 查询未分配任务总数
     */
    @Override
    public int queryTaskNoAssignCount(String regionCode, String branchCode, String storeCode, String cateCode,
            Long taskId) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("taskId", taskId);
        if (StringUtils.isNotBlank(branchCode)) {
            queryMap.put("branchCode", branchCode);
        }
        if (StringUtils.isNotBlank(regionCode)) {
            queryMap.put("regionCode", regionCode);
        }
        if (StringUtils.isNotBlank(storeCode)) {
            queryMap.put("storeCode", storeCode);
        }
        if (StringUtils.isNotBlank(cateCode)) {
            queryMap.put("cateCode", cateCode);
        }
        Map<String, Object> numMap = dalClient.queryForMap("MODEL_TASK.QUERY_TASK_NO_ASSIGN_COUNT", queryMap);
        return numMap.get("NUM") != null ? ((Number) numMap.get("NUM")).intValue() : 0;
    }

    @Override
    public List<ModelEffectiveness> queryModelEffectList(String regionCode, String branchCode, String comRangeName,
            String comGroupName, String commCode, String storeCode,String brandCode,String categoryCode,
            Integer pageNo, Integer pageSize) {
        Map<String, Object> queryMap = createQueryModelEffectMap(regionCode, branchCode, comRangeName, 
                comGroupName, commCode, storeCode, brandCode, categoryCode);
        queryMap.put("startIndex", pageSize * (pageNo - 1));
        queryMap.put("maxCount", pageSize);
        return convertModelEffectList(
                dalClient.queryForList("MODEL_MAIN_LIST.SELECT_MODEL_EFFECT_LIST", queryMap, ModelEffectiveness.class));
    }
    
    @Override
    public List<ModelEffectiveness> queryModelEffectListEx(String regionCode, String branchCode, String comRangeName,
            String comGroupName, String commCode, String storeCode, String brandCode, String categoryCode,
            Integer pageNo, Integer pageSize, String month, String storeType, String searchType) {
        Map<String, Object> queryMap = createQueryModelEffectMapEx(regionCode, branchCode, comRangeName, comGroupName,
                commCode, storeCode, brandCode, categoryCode, month, storeType);
        if (null != pageSize && null != pageNo) {
            queryMap.put("startIndex", pageSize * (pageNo - 1));
            queryMap.put("maxCount", pageSize);
        }
        if (EXPORT_SEARCH.equals(searchType)) {
            queryMap.put("storeTypes", 1);
        }
        return convertModelEffectListEx(dalClient.queryForList("MODEL_MAIN_LIST.SELECT_MODEL_EFFECT_LIST_NEW",
                queryMap, ModelEffectiveness.class), month);
    }

    /**
     * 返回空的数据,默认置0 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param es
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private List<ModelEffectiveness> convertModelEffectList(List<ModelEffectiveness> es) {
        if (CollectionUtils.isNotEmpty(es)) {
            for (ModelEffectiveness me : es) {
                if (null == me.getSaleCount1()) {
                    me.setSaleCount1(DEFALUT_COUNT);
                }
                if (null == me.getSaleCount2()) {
                    me.setSaleCount2(DEFALUT_COUNT);
                }
                if (null == me.getSaleCount3()) {
                    me.setSaleCount3(DEFALUT_COUNT);
                }
                if (null == me.getSaleCount4()) {
                    me.setSaleCount4(DEFALUT_COUNT);
                }
                if (null == me.getSaleCount5()) {
                    me.setSaleCount5(DEFALUT_COUNT);
                }
                if (null == me.getSaleCount6()) {
                    me.setSaleCount6(DEFALUT_COUNT);
                }
                if (null == me.getSaleCount7()) {
                    me.setSaleCount7(DEFALUT_COUNT);
                }
                if (null == me.getSaleCount8()) {
                    me.setSaleCount8(DEFALUT_COUNT);
                }
                if (null == me.getSaleCount9()) {
                    me.setSaleCount9(DEFALUT_COUNT);
                }
                if (null == me.getSaleCount10()) {
                    me.setSaleCount10(DEFALUT_COUNT);
                }
                if (null == me.getSaleCount11()) {
                    me.setSaleCount11(DEFALUT_COUNT);
                }
                if (null == me.getSaleCount12()) {
                    me.setSaleCount12(DEFALUT_COUNT);
                }
                if (null == me.getModelCount()) {
                    me.setModelCount(DEFALUT_COUNT);
                }
                if (null == me.getComGroupName()) {
                    me.setComGroupName("");
                }
                if (null == me.getComRangeName()) {
                    me.setComRangeName("");
                }
            }
        }
        return es;
    }
    
    private List<ModelEffectiveness> convertModelEffectListEx(List<ModelEffectiveness> es, String month) {
        int thisMonth = 1;
        Calendar c = Calendar.getInstance();
        if (StringUtils.isNotBlank(month)) {
            String[] monthArr = month.split("-");
            String monthVal = monthArr[monthArr.length - 1];
            if (StringUtils.isNumeric(monthVal)) {
                thisMonth = new java.math.BigDecimal(monthVal).intValue();
            } else {
                thisMonth = c.get(Calendar.MONTH) + 1;
            }
        } else {
            thisMonth = c.get(Calendar.MONTH) + 1;
        }

        if (CollectionUtils.isNotEmpty(es)) {
            for (ModelEffectiveness me : es) {
                switch (thisMonth) {
                    case 1:
                        me.setSaleCount(me.getSaleCount1() == null ? DEFALUT_COUNT : me.getSaleCount1());
                        break;
                    case 2:
                        me.setSaleCount(me.getSaleCount2() == null ? DEFALUT_COUNT : me.getSaleCount2());
                        break;
                    case 3:
                        me.setSaleCount(me.getSaleCount3() == null ? DEFALUT_COUNT : me.getSaleCount3());
                        break;
                    case 4:
                        me.setSaleCount(me.getSaleCount4() == null ? DEFALUT_COUNT : me.getSaleCount4());
                        break;
                    case 5:
                        me.setSaleCount(me.getSaleCount5() == null ? DEFALUT_COUNT : me.getSaleCount5());
                        break;
                    case 6:
                        me.setSaleCount(me.getSaleCount6() == null ? DEFALUT_COUNT : me.getSaleCount6());
                        break;
                    case 7:
                        me.setSaleCount(me.getSaleCount7() == null ? DEFALUT_COUNT : me.getSaleCount7());
                        break;
                    case 8:
                        me.setSaleCount(me.getSaleCount8() == null ? DEFALUT_COUNT : me.getSaleCount8());
                        break;
                    case 9:
                        me.setSaleCount(me.getSaleCount9() == null ? DEFALUT_COUNT : me.getSaleCount9());
                        break;
                    case 10:
                        me.setSaleCount(me.getSaleCount10() == null ? DEFALUT_COUNT : me.getSaleCount10());
                        break;
                    case 11:
                        me.setSaleCount(me.getSaleCount11() == null ? DEFALUT_COUNT : me.getSaleCount11());
                        break;
                    case 12:
                        me.setSaleCount(me.getSaleCount12() == null ? DEFALUT_COUNT : me.getSaleCount12());
                        break;
                    default:
                        me.setSaleCount(DEFALUT_COUNT);
                        break;
                }
                if (null == me.getModelCount()) {
                    me.setModelCount(DEFALUT_COUNT);
                }
                if (null == me.getComGroupName()) {
                    me.setComGroupName("");
                }
                if (null == me.getComRangeName()) {
                    me.setComRangeName("");
                }
            }
        }
        return es;
    }

    private Map<String, Object> createQueryModelEffectMap(String regionCode, String branchCode, String comRangeName,
            String comGroupName, String commCode, String storeCode,String brandCode,String categoryCode) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("disCode", StringUtils.isNotBlank(regionCode) ? regionCode : null);
        queryMap.put("branchCode", StringUtils.isNotBlank(branchCode) ? branchCode : null);
        if (StringUtils.isNotBlank(comRangeName)) {
            queryMap.put("comRangeName", "%"+comRangeName+"%");
        }
        if (StringUtils.isNotBlank(comGroupName)) {
            queryMap.put("comGroupName", "%"+comGroupName+"%");
        }
        queryMap.put("commCode", StringUtils.isNotBlank(commCode) ? commCode : null);
        queryMap.put("cateCode", StringUtils.isNotBlank(categoryCode) ? categoryCode : null);
        queryMap.put("branch9code", StringUtils.isNotBlank(brandCode) ? brandCode : null);
        queryMap.put("storeCode", storeCode);
        Calendar c = Calendar.getInstance();
        int month = c.get(Calendar.MONTH) + 1;
        switch (month) {
            case 1:
                queryMap.put("saleCount1", 1);
                break;
            case 2:
                queryMap.put("saleCount2", 1);
                break;
            case 3:
                queryMap.put("saleCount3", 1);
                break;
            case 4:
                queryMap.put("saleCount4", 1);
                break;
            case 5:
                queryMap.put("saleCount5", 1);
                break;
            case 6:
                queryMap.put("saleCount6", 1);
                break;
            case 7:
                queryMap.put("saleCount7", 1);
                break;
            case 8:
                queryMap.put("saleCount8", 1);
                break;
            case 9:
                queryMap.put("saleCount9", 1);
                break;
            case 10:
                queryMap.put("saleCount10", 1);
                break;
            case 11:
                queryMap.put("saleCount11", 1);
                break;
            case 12:
                queryMap.put("saleCount12", 1);
                break;
        }
        return queryMap;
    }
    
    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param regionCode
     * @param branchCode
     * @param comRangeName
     * @param comGroupName
     * @param commCode
     * @param storeCode
     * @param brandCode
     * @param categoryCode
     * @param month 格式：2017-06
     * @param storeType 0服务站，1门店
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private Map<String, Object> createQueryModelEffectMapEx(String regionCode, String branchCode, String comRangeName,
            String comGroupName, String commCode, String storeCode,String brandCode,String categoryCode, String month, String storeType) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("disCode", StringUtils.isNotBlank(regionCode) ? regionCode : null);
        queryMap.put("branchCode", StringUtils.isNotBlank(branchCode) ? branchCode : null);
        if (StringUtils.isNotBlank(comRangeName)) {
            queryMap.put("comRangeName", "%"+comRangeName+"%");
        }
        if (StringUtils.isNotBlank(comGroupName)) {
            queryMap.put("comGroupName", "%"+comGroupName+"%");
        }
        queryMap.put("commCode", StringUtils.isNotBlank(commCode) ? commCode : null);
        queryMap.put("cateCode", StringUtils.isNotBlank(categoryCode) ? categoryCode : null);
        queryMap.put("branch9code", StringUtils.isNotBlank(brandCode) ? brandCode : null);
        queryMap.put("storeCode", storeCode);
        Calendar c = Calendar.getInstance();
        // 查询月份
        int thisMonth = 1;
        if (StringUtils.isNotBlank(month)) {
            String[] monthArr = month.split("-");
            String monthVal = monthArr[monthArr.length - 1];
            if (StringUtils.isNumeric(monthVal)) {
                thisMonth = new java.math.BigDecimal(monthVal).intValue();
            } else {
                thisMonth = c.get(Calendar.MONTH) + 1;
            }
        } else {
            thisMonth = c.get(Calendar.MONTH) + 1;
        }
        switch (thisMonth) {
            case 1:
                queryMap.put("saleCount1", 1);
                break;
            case 2:
                queryMap.put("saleCount2", 1);
                break;
            case 3:
                queryMap.put("saleCount3", 1);
                break;
            case 4:
                queryMap.put("saleCount4", 1);
                break;
            case 5:
                queryMap.put("saleCount5", 1);
                break;
            case 6:
                queryMap.put("saleCount6", 1);
                break;
            case 7:
                queryMap.put("saleCount7", 1);
                break;
            case 8:
                queryMap.put("saleCount8", 1);
                break;
            case 9:
                queryMap.put("saleCount9", 1);
                break;
            case 10:
                queryMap.put("saleCount10", 1);
                break;
            case 11:
                queryMap.put("saleCount11", 1);
                break;
            case 12:
                queryMap.put("saleCount12", 1);
                break;
        }
        // 门店类型storeType
        if (StringUtils.isNotBlank(storeType)) {
            List<String> types = new ArrayList<String>();
            // 门店
            if (String.valueOf(ModelConstant.STORE_TYPE_ELECTRIC).equals(storeType)) {
                types.add(ModelConstant.STORE_TYPE_INFOS.STORE_TYPE_JQD);
                types.add(ModelConstant.STORE_TYPE_INFOS.STORE_TYPE_SNYSD);
                types.add(ModelConstant.STORE_TYPE_INFOS.STORE_TYPE_CGD);
                types.add(ModelConstant.STORE_TYPE_INFOS.STORE_TYPE_D4LSQD);
                types.add(ModelConstant.STORE_TYPE_INFOS.STORE_TYPE_EXPOCJD);
                types.add(ModelConstant.STORE_TYPE_INFOS.STORE_TYPE_BZD);
                types.add(ModelConstant.STORE_TYPE_INFOS.STORE_TYPE_DQDQJD);
                types.add(ModelConstant.STORE_TYPE_INFOS.STORE_TYPE_YD);
                types.add(ModelConstant.STORE_TYPE_INFOS.STORE_TYPE_CTD);
                types.add(ModelConstant.STORE_TYPE_INFOS.STORE_TYPE_QJD);
                types.add(ModelConstant.STORE_TYPE_INFOS.STORE_TYPE_DQQJD);
                types.add(ModelConstant.STORE_TYPE_INFOS.STORE_TYPE_SQD);
                types.add(ModelConstant.STORE_TYPE_INFOS.STORE_TYPE_SNSHGCD);
                types.add(ModelConstant.STORE_TYPE_INFOS.STORE_TYPE_EXPOCJQJD);
                types.add(ModelConstant.STORE_TYPE_INFOS.STORE_TYPE_DQD);
                types.add(ModelConstant.STORE_TYPE_INFOS.STORE_TYPE_CJD);
                queryMap.put("storeTypes", types);
            }
            // 服务站
            if (String.valueOf(ModelConstant.STORE_TYPE_SERVICE).equals(storeType)) {
                types.add(ModelConstant.STORE_TYPE_INFOS.STORE_TYPE_FWZ);
                queryMap.put("storeTypes", types);
            }
        }
        return queryMap;
    }
    
    @Override
    public int queryModelEffectCount(String regionCode, String branchCode, String comRangeName,
            String comGroupName, String commCode, String storeCode,String brandCode,String categoryCode) {
        Map<String, Object> queryMap = createQueryModelEffectMap(regionCode, branchCode, comRangeName, 
                comGroupName, commCode, storeCode, brandCode, categoryCode);
        Map<String, Object> numMap = dalClient.queryForMap("MODEL_MAIN_LIST.SELECT_MODEL_EFFECT_LIST_COUNT", queryMap);
        return numMap.get("NUM") != null ? ((Number) numMap.get("NUM")).intValue() : 0;
    }
    
    /**
     * 查询样机有效性列表数量
     */
    @Override
    public int queryModelEffectCountEx(String regionCode, String branchCode, String comRangeName,
            String comGroupName, String commCode, String storeCode,String brandCode,String categoryCode, String month, String storeType) {
        Map<String, Object> queryMap = createQueryModelEffectMapEx(regionCode, branchCode, comRangeName, 
                comGroupName, commCode, storeCode, brandCode, categoryCode, month, storeType);
        Map<String, Object> numMap = dalClient.queryForMap("MODEL_MAIN_LIST.SELECT_MODEL_EFFECT_LIST_COUNT_NEW", queryMap);
        return numMap.get("NUM") != null ? ((Number) numMap.get("NUM")).intValue() : 0;
    }

    @Override
    public int deleteModelCluster(Integer clusterId, String commCode, String branchCode) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("orgCode", branchCode);
        int result = 0;
        if (clusterId != null) {
            int clusterCount = queryClusterCommCount(clusterId, null, branchCode);
            if (clusterCount > 2) {
                queryMap.put("commCode", StringUtils.isNotBlank(commCode) ? commCode : null);
                result = 1;
            }
            queryMap.put("clusterCode", clusterId);
            dalClient.execute("MODEL_COMMODITY_CLUSTER.DELETE_CLUSTER_BY_CLUSTER_CODE", queryMap);
        } else {
            queryMap.put("commCode", commCode);
            dalClient.execute("MODEL_COMMODITY_CLUSTER.DELETE_NO_CLUSTER_BY_COMM_CODE", queryMap);
        }
        return result;
    }

    @Override
    public List<CommInfoBean> queryClusterCommList(Integer clusterId, String commCode, String branchCode,
            Integer pageSize, Integer pageNo) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("orgCode", branchCode);
        queryMap.put("clusterCode", clusterId);
        queryMap.put("startIndex", pageSize * (pageNo - 1));
        queryMap.put("maxCount", pageSize);
        if (StringUtils.isNotBlank(commCode)) {
            queryMap.put("commCode", commCode);
            return dalClient.queryForList("MODEL_COMMODITY_CLUSTER.QUERY_NOCLUSTER_COMM_BY_COMM_CODE", queryMap,
                    CommInfoBean.class);
        }
        return dalClient.queryForList("MODEL_COMMODITY_CLUSTER.QUERY_CLUSTER_COMM_BY_CLUSTER_CODE", queryMap,
                CommInfoBean.class);
    }

    @Override
    public int queryClusterCommCount(Integer clusterId, String commCode, String branchCode) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("orgCode", branchCode);
        queryMap.put("clusterCode", clusterId);
        Map<String, Object> numMap = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(commCode)) {
            queryMap.put("commCode", commCode);
            numMap = dalClient.queryForMap("MODEL_COMMODITY_CLUSTER.QUERY_NOCLUSTER_COUNT_BY_COMM_CODE", queryMap);
        } else {
            numMap = dalClient.queryForMap("MODEL_COMMODITY_CLUSTER.QUERY_CLUSTER_COUNT_BY_CLUSTER_CODE", queryMap);
        }
        return numMap.get("NUM") != null ? ((Number) numMap.get("NUM")).intValue() : 0;
    }

    @Override
    public void deleteNoClusterCommByTime(String updateTime) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("updateTime", updateTime);
        dalClient.execute("MODEL_COMMODITY_CLUSTER.DELETE_NOCLUSTER_BY_TIME", queryMap);
    }

    @Override
    public void deleteModelSaleExptByTime(String updateTime) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("updateTime", updateTime);
        dalClient.execute("MODEL_MAIN_LIST.DELETE_SALE_MODEL_EXPT_BY_TIME", queryMap);
    }

    @Override
    public void updateTaskStatusByTaskId(List<Long> taskId, Integer taskStatus) {
        Map<String, Object> updateMap = new HashMap<String, Object>();
        updateMap.put("taskIds", taskId);
        updateMap.put("taskStatus", taskStatus);
        dalClient.execute("MODEL_TASK.UPDATE_TASK_STATUS_BY_ID", updateMap);
    }

    @Override
    public List<Long> queryTaskIdByTime(String time, Integer type) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        if (type == 1) {
            queryMap.put("startTimeLimit", time);
            queryMap.put("unequalStatus", ModelConstant.TASK_RUN_STATUS_ING);
        } else {
            queryMap.put("endTimeLimit", time);
            queryMap.put("unequalStatus", ModelConstant.TASK_RUN_STATUS_END);
        }
        return dalClient.queryForList("MODEL_TASK.QUERY_TASK_ID_BY_CON", queryMap, Long.class);
    }

    @Override
    public int queryAssignTaskCount(Long taskId) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("taskId", taskId);
        Map<String, Object> numMap = dalClient.queryForMap("MODEL_TASK.QUERY_ASSIGN_TASK_COUNT_BY_ID", queryMap);
        return numMap.get("NUM") != null ? ((Number) numMap.get("NUM")).intValue() : 0;
    }

    @Override
    public void batchUpdateAssignTaskStatus(Map<String, Object>[] maps) {
        dalClient.batchUpdate("MODEL_TASK.UPDATE_ASSIGN_TASK_STATUS", maps);
    }

    @Override
    public List<Map<String, Object>> queryAssignTaskById(Long taskId, Integer startIndex, Integer limit) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("taskEndStatus", ModelConstant.TASK_RUN_STATUS_END);
        queryMap.put("taskId", taskId);
        queryMap.put("startIndex", startIndex);
        queryMap.put("maxCount", limit);
        return dalClient.queryForList("MODEL_TASK.QUERY_ASSIGN_TASK_BY_ID", queryMap);
    }

    @Override
    public List<TaskInfoPart> queryTaskIdByStatus(Integer taskStatus, Integer createStatus, Long taskId) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("taskStatus", taskStatus);
        queryMap.put("createStatus", createStatus);
        if (taskId != null) {
            queryMap.put("taskId", taskId);
        }
        return dalClient.queryForList("MODEL_TASK.QUERY_TASK_MAIN_BY_CON", queryMap, TaskInfoPart.class);
    }

    @Override
    public List<ModelTaskStoreBean> queryTaskStoreList(Long taskId) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("taskId", taskId);
        return dalClient.queryForList("MODEL_TASK.QUERY_TASK_STORE", queryMap, ModelTaskStoreBean.class);
    }

    @Override
    public List<String> queryStoreBrandCode(String storeCode) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("storeCode", storeCode);
        return dalClient.queryForList("MODEL_MAIN_LIST.QUERY_STORE_BRAND_CODE", queryMap, String.class);
    }

    @Override
    public void batchInsertTaskAssign(Map<String, Object>[] maps) {
        dalClient.batchUpdate("MODEL_TASK.INSERT_TASK_ASSIGN", maps);
    }

    @Override
    public void deleteNoCluster(String mainCommCode, String commCode, String branchCode) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("mainCommCode", mainCommCode);
        queryMap.put("commCode", commCode);
        queryMap.put("orgCode", branchCode);
        dalClient.execute("MODEL_COMMODITY_CLUSTER.DELETE_NO_CLUSTER_BY_COMMS", queryMap);
    }

    @Override
    public List<String> queryCateBrandByStoreCode(String storeCode) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("storeCode", storeCode);
        return dalClient.queryForList("MODEL_MAIN_LIST.QUERY_BRAND_CODE_BY_STORE_CODE", queryMap, String.class);
    }

    @Override
    public List<String> queryBrandByStoreCode(String storeCode) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("storeCode", storeCode);
        return dalClient.queryForList("MODEL_MAIN_LIST.QUERY_MODEL_LIST_BRAND_CODE_BY_STORE", queryMap, String.class);
    }

    @Override
    public void deleteStoreBrand(String storeCode, List<String> branchCodeList) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("storeCode", storeCode);
        queryMap.put("branchCodeList", branchCodeList);
        dalClient.execute("MODEL_MAIN_LIST.DELETE_CATE_BRAND_BY_STORE_BRAND", queryMap);
    }

    @Override
    public List<String> queryCateBrandStoreList() {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        return dalClient.queryForList("MODEL_MAIN_LIST.QUERY_CATE_BRAND_STORE_CODE", queryMap, String.class);
    }

    /**
     * 查询任务品类品牌
     */
    @Override
    public List<BrandBean> queryTaskCateBrands(Long taskId) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("taskId", taskId);
        return dalClient.queryForList("MODEL_MAIN_LIST.QUERY_TASK_CATE_BRAND_BY_TASKID", queryMap, BrandBean.class);
    }

    /**
     * 根据条件查询已分配任务总数
     */
    @Override
    public int queryTaskHasAssignCount(Long taskId, String regionCode, String branchCode, String storeCode,
            String cateCode, String brandCode, String taskStatus) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("taskId", taskId);
        if (StringUtils.isNotBlank(branchCode)) {
            queryMap.put("branchCode", branchCode);
        }
        if (StringUtils.isNotBlank(regionCode)) {
            queryMap.put("regionCode", regionCode);
        }
        if (StringUtils.isNotBlank(storeCode)) {
            queryMap.put("storeCode", storeCode);
        }
        if (StringUtils.isNotBlank(cateCode)) {
            queryMap.put("cateCode", cateCode);
        }
        if (StringUtils.isNotBlank(brandCode)) {
            queryMap.put("brandCode", brandCode);
        }
        if (StringUtils.isNotBlank(taskStatus)) {
            queryMap.put("checkStatus", taskStatus);
        }
        Map<String, Object> numMap = dalClient.queryForMap("MODEL_TASK.QUERY_HAS_ASSIGN_TASK_COUNT_BY_ID", queryMap);
        return numMap.get("NUM") != null ? ((Number) numMap.get("NUM")).intValue() : 0;
    }

    /**
     * 根据条件查询已分配任务列表
     */
    @Override
    public List<ModelTaskAssignDetailsBean> queryTaskHasAssignList(Long taskId, String regionCode, String branchCode,
            String storeCode, String cateCode, String brandCode, String taskStatus, int pageSize, int pageNo) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        // 处理页码
        int start = pageSize * (pageNo - 1);
        if (start < 0) {
            start = 0;
        }
        queryMap.put("taskId", taskId);
        queryMap.put("startIndex", start);
        queryMap.put("maxCount", pageSize);
        if (StringUtils.isNotBlank(branchCode)) {
            queryMap.put("branchCode", branchCode);
        }
        if (StringUtils.isNotBlank(regionCode)) {
            queryMap.put("regionCode", regionCode);
        }
        if (StringUtils.isNotBlank(storeCode)) {
            queryMap.put("storeCode", storeCode);
        }
        if (StringUtils.isNotBlank(cateCode)) {
            queryMap.put("cateCode", cateCode);
        }
        if (StringUtils.isNotBlank(brandCode)) {
            queryMap.put("brandCode", brandCode);
        }
        if (StringUtils.isNotBlank(taskStatus)) {
            queryMap.put("checkStatus", taskStatus);
        }
        return dalClient.queryForList("MODEL_TASK.QUERY_HAS_ASSIGN_TASK_BY_ID", queryMap,
                ModelTaskAssignDetailsBean.class);
    }

    
    @Override
    public int queryTaskAssignAllCount(Long taskId, String regionCode, String branchCode, String storeCode,
            String cateCode, String brandCode, String taskStatus, String assignStatus) {

        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("taskId", taskId);
        if (StringUtils.isNotBlank(branchCode)) {
            queryMap.put("branchCode", branchCode);
        }
        if (StringUtils.isNotBlank(regionCode)) {
            queryMap.put("regionCode", regionCode);
        }
        if (StringUtils.isNotBlank(storeCode)) {
            queryMap.put("storeCode", storeCode);
        }
        if (StringUtils.isNotBlank(cateCode)) {
            queryMap.put("cateCode", cateCode);
        }
        if (StringUtils.isNotBlank(brandCode)) {
            queryMap.put("brandCode", brandCode);
        }
        if (StringUtils.isNotBlank(taskStatus)) {
            queryMap.put("checkStatus", taskStatus);
        }
        if (StringUtils.isNotBlank(assignStatus)) {
            queryMap.put("assignStatus", assignStatus);
        }
        Map<String, Object> numMap = dalClient.queryForMap("MODEL_TASK.QUERY_ALL_ASSIGN_TASK_COUNT_BY_ID", queryMap);
        return numMap.get("NUM") != null ? ((Number) numMap.get("NUM")).intValue() : 0;
    
    }

    @Override
    public List<ModelTaskAssignDetailsBean> queryAssignAllList(Long taskId, String regionCode, String branchCode,
            String storeCode, String cateCode, String brandCode, String taskStatus, String assignStatus, int pageSize,
            int start) {

        Map<String, Object> queryMap = new HashMap<String, Object>();
 
        queryMap.put("taskId", taskId);
        queryMap.put("startIndex", start);
        queryMap.put("maxCount", pageSize);
        if (StringUtils.isNotBlank(branchCode)) {
            queryMap.put("branchCode", branchCode);
        }
        if (StringUtils.isNotBlank(regionCode)) {
            queryMap.put("regionCode", regionCode);
        }
        if (StringUtils.isNotBlank(storeCode)) {
            queryMap.put("storeCode", storeCode);
        }
        if (StringUtils.isNotBlank(cateCode)) {
            queryMap.put("cateCode", cateCode);
        }
        if (StringUtils.isNotBlank(brandCode)) {
            queryMap.put("brandCode", brandCode);
        }
        if (StringUtils.isNotBlank(taskStatus)) {
            queryMap.put("checkStatus", taskStatus);
        }
        
        if (StringUtils.isNotBlank(assignStatus)) {
            queryMap.put("assignStatus", assignStatus);
        }
        return dalClient.queryForList("MODEL_TASK.QUERY_All_ASSIGN_TASK_BY_ID", queryMap,
                ModelTaskAssignDetailsBean.class);
    
    }
    
    /**
     * 查询指定督导总数
     */
    @Override
    public int queryAppointTaskTotal(String staffId) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("staffId", staffId);
        Map<String, Object> numMap = dalClient.queryForMap("MODEL_TASK.QUERY_APPOINT_TASK_TOTAL", queryMap);
        return numMap.get("NUM") != null ? ((Number) numMap.get("NUM")).intValue() : 0;
    }

    /**
     * 查询指定督导分配数据
     */
    @Override
    public List<ModelTaskAssignDetailsBean> queryAppointTaskPage(String staffId, int currIndex, int size) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("staffId", staffId);
        queryMap.put("currIndex", currIndex);
        queryMap.put("size", size);
        return dalClient.queryForList("MODEL_TASK.QUERY_APPOINT_TASK_PAGE", queryMap,
                ModelTaskAssignDetailsBean.class);
    }

    /**
     * 删除指定督导数据
     */
    @Override
    public void clearAppointTask(Long taskId, String staffId, String storeCode, String cateCode) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("taskId", taskId);
        queryMap.put("staffId", staffId);
        queryMap.put("storeCode", storeCode);
        queryMap.put("cateCode", cateCode);
        dalClient.execute("MODEL_TASK.CLEAR_APPOINT_TASK", queryMap);
    }

    /**
     * 清空簇商品表 MODEL_COMMODITY_CLUSTER
     */
    @Override
    public void clearModelCluster() {
        dalClient.execute("MODEL_COMMODITY_CLUSTER.CLEAR_MODEL_COMMODITY_CLUSTER", null);
    }

    /**
     * 批量插入簇商品信息
     */
    @Override
    public void batchSaveCluster(Map<String, Object>[] maps) {
        dalClient.batchUpdate("MODEL_COMMODITY_CLUSTER.BATCH_INSERT_MODEL_COMMODITY_CLUSTER", maps);
    }

    /**
     * 查询所有的商品系列名称和簇编码
     */
    @Override
    public List<ModelClusterInfo> queryClusterRangeNameClusterCode() {
        List<ModelClusterInfo> list = new ArrayList<ModelClusterInfo>();
        list = dalClient.queryForList("MODEL_COMMODITY_CLUSTER.QUERY_CLUSTER_RANGENAME_CLUSTERCODE", null,
                ModelClusterInfo.class);
        return list;
    }

    /**
     * 根据查询条件获取样机列表
     */
    @Override
    public List<ModelBean> queryModelBySearchField(String regionCode, String branchCode, String commCode,
            String storeCode, Integer pageNo, Integer pageSize) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("regionCode", regionCode);
        if (StringUtils.isNotBlank(branchCode)) {
            queryMap.put("branchCode", branchCode);
        }
        if (StringUtils.isNotBlank(commCode)) {
            queryMap.put("commCode", commCode);
        }
        if (StringUtils.isNotBlank(storeCode)) {
            queryMap.put("storeCode", storeCode);
        }
        queryMap.put("startIndex", (pageNo - 1) * pageSize);
        queryMap.put("maxCount", pageSize);
        return dalClient.queryForList("MODEL_MAIN_LIST.QUERY_BY_SEARCH_FIELD", queryMap, ModelBean.class);
    }
    
    @Override
    public List<ModelBean> queryModelBySearchField(String regionCode, String branchCode, String commCode,
            String storeCode, Integer pageNo, Integer pageSize, String status, String categoryCode, String brandCode) {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("regionCode", regionCode);
        if (StringUtils.isNotBlank(branchCode)) {
            queryMap.put("branchCode", branchCode);
        }
        if (StringUtils.isNotBlank(commCode)) {
            queryMap.put("commCode", commCode);
        }
        if (StringUtils.isNotBlank(storeCode)) {
            queryMap.put("storeCode", storeCode);
        }
        if (StringUtils.isNotBlank(status)) {
            queryMap.put("status", status);
        }
        if (StringUtils.isNotBlank(categoryCode)) {
            queryMap.put("categoryCode", categoryCode);
        }
        if (StringUtils.isNotBlank(brandCode)) {
            queryMap.put("brandCode", brandCode);
        }
        queryMap.put("startIndex", (pageNo - 1) * pageSize);
        queryMap.put("maxCount", pageSize);
        return dalClient.queryForList("MODEL_MAIN_LIST.QUERY_BY_SEARCH_FIELD_EX", queryMap, ModelBean.class);
    }
    
    @Override
    public List<ModelTaskBean> queryExistTaskIdsByStoreCode(String storeCode, String brand9Code) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        paramMap.put("brand9Code", brand9Code);
        paramMap.put("runStatus", ModelConstant.TASK_RUN_STATUS_ING);
        return dalClient.queryForList("MODEL_TASK.QUERY_RUNNING_TASK_ID", paramMap, ModelTaskBean.class);
    }

    @Override
    public Integer queryClusterDataTotal() {
        Map<String, Object> numMap = dalClient.queryForMap("MODEL_COMMODITY_CLUSTER.QUERY_CLUSTER_COUNT", null);
        return numMap.get("NUM") != null ? ((Number) numMap.get("NUM")).intValue() : 0;
    }

    @Override
    public int queryTaskMonitorCount(String branchCode, String regionCode, Long taskId) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public List<ModelTaskMonitorDetailVo> queryTaskMonitorList(String branchCode, String regionCode, Long taskId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("taskId", taskId);
        //查询分公司
        if (StringUtils.isNotBlank(branchCode)) {
            paramMap.put("branchCode", branchCode);
            return dalClient.queryForList("MODEL_MONITOR.QUERY_MONITOR_TASK_BY_ID_BR", paramMap,
                    ModelTaskMonitorDetailVo.class);
        }
        
        //查询大区
        if (StringUtils.isNotBlank(regionCode)) {
            paramMap.put("regionCode", regionCode);
            return dalClient.queryForList("MODEL_MONITOR.QUERY_MONITOR_TASK_BY_ID_RE", paramMap,
                    ModelTaskMonitorDetailVo.class);
        }
        
        return dalClient.queryForList("MODEL_MONITOR.QUERY_MONITOR_TASK_BY_ID_HQ", paramMap,
                ModelTaskMonitorDetailVo.class);
    }


    @Override
    public List<ModelTaskMonitorDetailVo> queryModelCheckList(String branchCode, String regionCode, Long taskId) {

        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("taskId", taskId);
        //查询分公司
        if (StringUtils.isNotBlank(branchCode)) {
            paramMap.put("branchCode", branchCode);
            return dalClient.queryForList("MODEL_MONITOR.QUERY_MONITOR_CHECK_LIST_BR", paramMap,
                    ModelTaskMonitorDetailVo.class);
        }
        
        //查询大区
        if (StringUtils.isNotBlank(regionCode)) {
            paramMap.put("regionCode", regionCode);
            return dalClient.queryForList("MODEL_MONITOR.QUERY_MONITOR_CHECK_LIST_RE", paramMap,
                    ModelTaskMonitorDetailVo.class);
        }
        
        return dalClient.queryForList("MODEL_MONITOR.QUERY_MONITOR_CHECK_LIST_HQ", paramMap,
                ModelTaskMonitorDetailVo.class);
    
    }

    @Override
    public List<ModelTaskMonitorDetailVo> queryModelTotalList(String branchCode, String regionCode, Long taskId) {

        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("taskId", taskId);
        //查询分公司
        if (StringUtils.isNotBlank(branchCode)) {
            paramMap.put("branchCode", branchCode);
            return dalClient.queryForList("MODEL_MONITOR.QUERY_MONITOR_TOTAL_LIST_BR", paramMap,
                    ModelTaskMonitorDetailVo.class);
        }
        
        //查询大区
        if (StringUtils.isNotBlank(regionCode)) {
            paramMap.put("regionCode", regionCode);
            return dalClient.queryForList("MODEL_MONITOR.QUERY_MONITOR_TOTAL_LIST_RE", paramMap,
                    ModelTaskMonitorDetailVo.class);
        }
        
        return dalClient.queryForList("MODEL_MONITOR.QUERY_MONITOR_TOTAL_LIST_HQ", paramMap,
                ModelTaskMonitorDetailVo.class);
    
    }

    @Override
    public int inserModelMainHis(String lastMonth, int pageNo, String downLoadUrl) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("lastMonth", lastMonth);
        paramMap.put("pageNo", pageNo);
        paramMap.put("downLoadUrl", downLoadUrl);
       return  dalClient.execute("MODEL_MAIN_LIST_HIS.INSERT_MODEL_MAIN_LIST_HIS", paramMap);
    }

    @Override
    public List<DownloadModelListBean> queryDownloadMonths() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
       return  dalClient.queryForList("MODEL_MAIN_LIST_HIS.QUERY_MODEL_MAIN_LIST_HIS_MONTHS", paramMap,DownloadModelListBean.class);
    }
    
    @Override
    public List<DownloadModelListBean> queryDownloadUrlList(String month) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("month", month);
       return  dalClient.queryForList("MODEL_MAIN_LIST_HIS.QUERY_MODEL_MAIN_LIST_HIS_BY_MONTH", paramMap,DownloadModelListBean.class);
    }


}
